package com.atguigu.dao.impl;

import com.atguigu.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/20 20:05
 * @注释
 */
@Repository
public class BookDaoImpl implements BookDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer findPriceByBookId(int bookId) {
        String sql = "select price from t_book where id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, bookId);
    }

    @Override
    public void updataStock(int bookId) {
        String sql = "update t_book set stock = stock - ? where id = ?";
        jdbcTemplate.update(sql, bookId);
    }

    @Override
    public void updateBalance(int userId, Integer price) {
        String sql = "update t_book set balance = balance - ? where id = ?";
        jdbcTemplate.update(sql, price, userId);
    }
}
